<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
class ShopController extends Controller
{
        public  function  index()
        {
              $product = DB::table('shop')
                    ->select('shop.*','type.name','hos_list.hos_name')
                    ->where('shop.is_del',0)
                   // ->where('product.city_id',$city_id->id)
                    ->leftJoin('type','shop.type_id','=','type.id')
                    ->leftJoin('hos_list','shop.hos_id','=','hos_list.id')
                    ->orderBy('hos_list.created_at','DESC')
                    ->get();
            
            return view('admin.shop.index',['list'=>$product]);
            
        }
        
        
        
        
        public  function create(Request $request)
        {
            // $users_id = $request->session()->get('user_id');
       
           // $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
        
            $list = \DB::select("select * from type  order by concat(path,id) asc");
        
            //处理信息
            foreach($list as &$v){
                $m = substr_count($v->path,","); //获取path中的逗号
                //生成缩进
                $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
            }
            
            $host = \DB::select("select * from hos_list");
            
            
           return view('admin.shop.create',['list'=>$list,'host'=>$host]);
           
           
        }
        
        public function store(Request $request)
    {
            
           // $users_id = $request->session()->get('user_id');
       
           // $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
            
            //获取指定的部分数据
         
            $data['pro_name'] = $request->input('product_name');
            $data['hos_id'] = $request->input('hos_id');
            $data['title'] = $request->input('title');
            $data['content'] = trim($request->input('content'));
            $data['type_id'] = $request->input('pid');
            $data['y_price'] = $request->input('y_price');
            $data['price'] = $request->input('price');
             $data['xuzhi'] = $request->input('xuzhi');
           // $data['city_id'] = $city_id->id;
             
            if(!$request->hasFile('pic')){
                 $request->session()->flash('error_msg','文件不存在');
                 return back();
             }
            $img = $request->file('pic');
            // 获取后缀名
            $ext = $img->extension();
            // 新文件名
            $saveName =time().rand().".".$ext;
            // 存储文件 已经不使用 move 这种方式
            // $img->move('./uploads/'.date('Ymd'),$saveName);
            // 使用 store 存储文件
            $data['img'] = $img->store(date('Ymd'));
            
            
            if(!$request->hasFile('p_img')){
                 $request->session()->flash('error_msg','文件不存在');
                 return back();
             }
            $img = $request->file('p_img');
            // 获取后缀名
            $ext = $img->extension();
            // 新文件名
            $saveName =time().rand().".".$ext;
            // 存储文件 已经不使用 move 这种方式
            // $img->move('./uploads/'.date('Ymd'),$saveName);
            // 使用 store 存储文件
            $data['p_img'] = $img->store(date('Ymd'));
            
            
            
            
            $data['created_at'] = date("Y-m-d H:i:s");
            
             $id = \DB::table("shop")->insertGetId($data);
        
           if($id>0){
                 return redirect('shop');
             }else{
                return back()->with("err","添加失败!");
             } 
    }
        
        public  function  add()
        {
             return view('admin.shop.add');
        }
        
        
        public function edit()
        {
            return view('admin.shop.edit');
        }
        
        
        public function update()
        {
             return view('admin.shop.update');
        }
        
        
        public function  show()
        {
            return view('admin.shop.show');
        }
        
        
        public function info()
        {
             return view('admin.shop.info');
        }
        
        
}
